/*
 * L9InputStream.cpp
 *
 *  Created on: 2011-10-18
 *      Author: baixp
 */

#include "../inc/L9InputStream.h"
#include "L9Util.h"
namespace lib9bada {
L9InputStream::L9InputStream(const char* file) {
	ifstream is(file, ios::binary);
	is.seekg(0, ios::end);
	_size = is.tellg();
	is.seekg(0, ios::beg);
	_buf = new byte[_size];
	is.read((char*)_buf, _size);
	is.close();
	_off = 0;
}
L9InputStream::L9InputStream(const byte* arr, int off, int len) {
	_buf = new byte[len];
	_off = 0;
	L9Util::System_arraycopy(arr, off, _buf, 0, len);
	_size = len;
}
L9InputStream::L9InputStream(const L9InputStream& in){
	_size = in._size;
	_buf = new byte[_size];
	_off = 0;
	L9Util::System_arraycopy(in._buf, 0, _buf, 0, _size);
}
L9InputStream& L9InputStream::operator=(const L9InputStream& in){
	if(this!=&in){//排除自赋值的情况
		if(_buf!=null){
			delete[] _buf;
		}
		_size = in._size;
		_buf = new byte[_size];
		_off = 0;
		L9Util::System_arraycopy(in._buf, 0, _buf, 0, _size);
	}
	return *this;
}
L9InputStream::~L9InputStream() {
	// TODO Auto-generated destructor stub
	if (_buf != NULL) {
		delete[] _buf;
	}
	_buf = NULL;
}

/**
 * 读出一个布尔值
 * @return boolean
 */
bool L9InputStream::readBoolean() {
	return readByte() == 1;
}

/**
 * 读出一个char字节
 * @return byte
 */
byte L9InputStream::readByte() {
	return _buf[_off++];
}

/**
 * 读取一个char字节返回为int值，参数bSigned为true表示有符号数,否则为无符号数，ReadByte(true)等于ReadByte()
 * @param bSigned boolean
 * @return int
 */
int L9InputStream::readByte(bool bSigned) {
	if (bSigned) {
		return readByte();
	}
	return readByte() & 0xFF;
}

/**
 * 读出len长度的字节数组
 * @param len int
 * @return byte*
 */
byte* L9InputStream::readBytesN(int len) {
	byte* rs = new byte[len];
	L9Util::System_arraycopy(_buf, _off, rs, 0, len);
	_off += len;
	return rs;
}

/**
 * 读出一个short值，占两个字节
 * @return short
 */
short L9InputStream::readShort() {
	short s = readShortLow(_buf, _off);
	_off += 2;
	return s;
}

/**
 * 读出一个int值，占4个字节
 * @return int
 */
int L9InputStream::readInt() {
	int s = readIntLow(_buf, _off);
	_off += 4;
	return s;
}

/**
 * 读出一个long值,占8个字节
 * @return long
 */
long L9InputStream::readLong() {
	long s = readLongLow(_buf, _off);
	_off += 8;
	return s;
}
/**
 * 读取采用UTF-8编码字符串
 */
String L9InputStream::readString() {
//	string desc = "";
//	int len = readInt();
//	if (len > 0) {
//		char* p = new char[len + 1];
//		L9Util::System_arraycopy(_buf, _off, p, 0, len);
//		p[len] = '\0';
//		desc = string(p);
//		delete[] p;
//
//		_off += len;
//	}
//	return desc;

	String desc = "";
	int len = readInt();
	if (len > 0) {
		byte* p = new byte[len + 1];
		L9Util::System_arraycopy(_buf, _off, p, 0, len);
		p[len] = '\0';
		desc = String((char*)p);
		delete[] p;

		_off += len;
	}
	return desc;
}
/**
 * 返回已经读取的字节数据，调用者负责释放内存
 * @return byte*
 */
byte* L9InputStream::getBytesN() {
	byte* pByte = NULL;
	if (_buf != NULL && _size > 0) {
		pByte = new byte[_size];
		L9Util::System_arraycopy(_buf, 0, pByte, 0, _size);
	}
	return pByte;
}
/**
 * 跳过nBytes字节数的位置
 * @param int nBytes
 */
void L9InputStream::skipBytes(int nBytes) {
	_off += nBytes;
}
/**
 * 返回当前指针的偏移量
 * @return int
 */
int L9InputStream::getOff() {
	return _off;
}

/**
 * 重新设置当前指针偏移量
 * @param off int
 */
void L9InputStream::setOff(int off) {
	_off = off;
}
/**
 * 返回输入的总得字节数
 */
int L9InputStream::getSize() {
	return _size;
}
};
